package cqrtplm.controller;

import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.file.handler.response.ResponseResult;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.PreventErrorCorrectionDTO;
import cqrtplm.dto.StartProblemItemTaskDTO;
import cqrtplm.entity.PreventErrorCorrectionDO;
import cqrtplm.service.PreventErrorCorrectionService;
import cqrtplm.util.PageDtoUtil;
import cqrtplm.vo.PreventErrorCorrectionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping(RouteConstant.CORRECTION + "v1")
@Api(tags = "预防纠错表")
public class PreventErrorCorrectionController {

    @Resource
    private PreventErrorCorrectionService preventErrorCorrectionService;

    @PostMapping("/getPreventErrorCorrectionList")
    public ResponseResult<PageInfo<PreventErrorCorrectionVO>> getPreventErrorCorrectionList(@RequestBody PreventErrorCorrectionDTO dto) {
        PageInfo<PreventErrorCorrectionVO> preventErrorCorrectionList = preventErrorCorrectionService.getPreventErrorCorrectionList(dto);
        return ResponseResult.success(preventErrorCorrectionList,  "查询预防纠正信息成功");
    }

    @PostMapping("/importPreventErrorCorrectionList")
    public ResponseResult<PageDtoUtil> importPreventErrorCorrectionList(
            @RequestParam("file")MultipartFile file) {
        PageDtoUtil preventErrorCorrectionDOPageInfo = preventErrorCorrectionService.importPreventErrorCorrection(file);
        return ResponseResult.success(preventErrorCorrectionDOPageInfo,  "插入预防纠正信息成功");
    }

    @PostMapping("deletePreventErrorCorrection")
    public void deletePreventErrorCorrection(@RequestBody PreventErrorCorrectionDTO dto) {
        preventErrorCorrectionService.deletePreventErrorCorrection(dto);
    }

    @PostMapping("startProblemItemTask")
    @ApiOperation(value = "问题事项处理并启动流程", produces = "application/json; charset=utf-8")
    public ResponseResult startProblemItemTask(@RequestBody StartProblemItemTaskDTO dto) {
        preventErrorCorrectionService.startProblemItemTask(dto);
        return ResponseResult.success("");
    }

    @PostMapping("/insertPreventErrorCorrectionFromProject")
    @ApiOperation(value = "插入问题到项目库", produces = "application/json; charset=utf-8")
    public ResponseResult insertPreventErrorCorrection(@RequestBody List<PreventErrorCorrectionDO> prevenList) {
        try {
            preventErrorCorrectionService.insertPreventErrorCorrection(prevenList);
        }catch (Exception e) {
            e.printStackTrace();
            throw RTErrorCodeEnum.ISUUE_REPORT_IMPORE_ERROR.getException(e.getMessage());

        }

        return ResponseResult.success("插入预防纠正信息成功");
    }

    @GetMapping("/getPreventErrorCorrection")
    public ResponseResult<PageInfo<PreventErrorCorrectionDO>> getPreventErrorCorrectionList(  @RequestParam(defaultValue = "1") int pageNum,
                                                                                              @RequestParam(defaultValue = "10") int pageSize,
                                                                                              @RequestParam(value = "projectId", required = false) String projectId) {
        PageInfo<PreventErrorCorrectionDO> preventErrorCorrectionList = preventErrorCorrectionService.getPreventErrorCorrection(pageNum,pageSize,projectId);
        return ResponseResult.success(preventErrorCorrectionList,  "查询预防纠正信息成功");
    }


    @PostMapping("/insertPreventCorrectionFromProblem")
    @ApiOperation(value = "插入问题到问题库", produces = "application/json; charset=utf-8")
    public ResponseResult<T> insertPreventCorrectionFromProblem(@RequestBody List<PreventErrorCorrectionDO> prevenList) {
        preventErrorCorrectionService.insertPreventCorrectionFromProblem(prevenList);
        return ResponseResult.success("插入预防纠正信息成功");
    }

    @GetMapping("/getAllProjectNo")
    public List<String> getAllProjectNo(){
        return preventErrorCorrectionService.getAllProjectNo();
    }
}
